﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Threading;


namespace CentralServer
{
    class Vehicle
    {
        string name;
        string address;
        //IPAddress ip;

        public Vehicle(string n, string addr)
        {
            name = n;
            address = addr;
        }

        public string Address()
        {
            return address;
        }

    }

    class MainServer
    {
        //static Vehicle kermit = new Vehicle("Kermit", "kermit.aw.cl.cam.ac.uk");
        static Vehicle kermit = new Vehicle("Kermit", "127.0.0.1");
        //static Interfaces.NetworkInterface input;
        static void Main(string[] args)
        {
            TcpListener listener = new TcpListener(IPAddress.Any, 9090);
            listener.Start();
            //input = new Interfaces.NetworkInterface();
            //new Thread(input.Runthis).Start();
            while (true)
            {
                Console.WriteLine("Server Running");
                TcpClient interfaceClient = listener.AcceptTcpClient();
                Console.WriteLine("Connection from:" + interfaceClient.ToString());
                StreamReader UIReader = new StreamReader(interfaceClient.GetStream());
                StreamWriter UIWriter = new StreamWriter(interfaceClient.GetStream());

                try
                {
                    while (!UIReader.EndOfStream)
                    {
                        string cmd = UIReader.ReadLine();

                        Console.WriteLine("UI DATA IN: " + cmd);
                        if (cmd == "AUTH: asdf")
                        {
                            UIWriter.WriteLine(kermit.Address());

                            UIWriter.Flush();
                        }
                        else
                        {
                            UIWriter.WriteLine("FAIL");
                            UIWriter.Flush();
                        }
                    }
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("Exception: " + e.StackTrace);
                }
            }
        }

        public Vehicle returnVehicle(string name)
        {
            return kermit;
        }
    }
}
